import { HttpStatus } from './types'

export class HttpResponse<T> {
  code: HttpStatus
  msg: string
  data: T
  constructor(httpStatus: HttpStatus, msg: string, data: T) {
    this.code = httpStatus
    this.msg = msg
    this.data = data
  }

  // 成功时返回数据,需要返回数据传参，不需要使用默认值
  static success(msg: string = '操作成功', data: any = null) {
    return new HttpResponse(HttpStatus.success, msg, data)
  }

  // 统一失败返回处理
  static fail(msg: string = '操作失败', data: any = null) {
    return new HttpResponse(HttpStatus.fail, msg, data)
  }

  // 主要返回请求参数的逻辑验证
  static validateFail(msg: string = '参数验证失败', data: any = null) {
    return new HttpResponse(HttpStatus.parameterErr, msg, data)
  }
}
